﻿
@{
    ViewBag.Title = "CoordFeeView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>协调费基础数据</legend>
</fieldset>
<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入协调内容" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            @*<div class="layui-form-mid layui-word-aux">高级搜索</div>*@
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增</button>
        </div>
    </div>
</form>
<table id="demo" lay-filter="ProjectTableList"></table>

<div id="EditLayer" style="display:none;">
    <form class="layui-form" id="EditFrom" style="margin-top:10px;">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>协调费信息</legend>
        </fieldset>
        <div class="layui-form-item">
            <input type="text" name="CoordFeeId" value="" style="display:none" />
            <div class="layui-inline">
                <label class="layui-form-label">协调内容</label>
                <div class="layui-input-inline" style="width:150px;">
                    <input type="text" name="CooContent" value="" placeholder="必填" required lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">协调部门</label>
                <div class="layui-input-inline" style="width:400px;">
                    <input type="text" name="CooDepart" value="" placeholder="必填" required lay-verify="required" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">协调金额</label>
                <div class="layui-input-inline" style="width:150px;">
                    <input type="number" name="money" value="" placeholder="必填" required lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline" style="width:400px;">
                    <input type="text" name="Memo" value=""  class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"></label>
            </div>
            <button type="button" class="layui-btn" id="BtnEditFromSave">保存</button>
        </div>
        <button type="submit" id="mysubmit" class="layui-btn" lay-submit lay-filter="formVerify" style="display:none;">立即提交</button>
    </form>
</div>


<script type="text/javascript">
    var table;
    layui.use(['form','table', 'upload', 'layer'], function () {
        var upload = layui.upload;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var form = layui.form;
        var index;
        var layerindex;
        var IsValidate = 0;


        //拦截form提交消息
        form.on('submit(formVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });

        $("#BtnAdd").on("click", function () {
            layerindex = layer.open({
                type: 1,
                title: ['新增', 'font-size:18px;'],
                area: ['900px', '300px'],
                content: $("#EditLayer"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                success: function (layero, index) {
                    $("#EditFrom input[name='CoordFeeId']").val("");
                }
            });
        });

        $("#BtnEditFromSave").on("click", function () {

            $("#mysubmit").click();
            if (IsValidate == 1) {
                IsValidate = 0;
            } else {
                return;
            }
            var obj = $("#EditFrom").serializeArray();
            var data = {};
            $.each(obj, function (i, v) {
                data[v.name] = v.value;
            });
            if (data["CoordFeeId"] == "") {
                delete data["CoordFeeId"];
                $.ajax({
                    type: "POST",
                    url: "/PreEstimate/CoordFeeAdd",
                    async: false,
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(data),
                    success: function (msg) {
                        if (msg.result == "OK") {
                            layer.msg('添加成功', { icon: 6, time: 1000 });
                            layer.close(layerindex);
                            table.reload("demo", {});
                        } else {
                            alert(msg.result);
                        }
                    }
                });
            }
            else {
                $.ajax({
                    type: "POST",
                    url: "/PreEstimate/CoordFeeUpdate",
                    async: false,
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(data),
                    success: function (msg) {
                        if (msg.result == "OK") {
                            layer.msg('修改成功', { icon: 6, time: 1000 });
                            layer.close(layerindex);
                            table.reload("demo", {});
                        } else {
                            alert(msg.result);
                        }
                    }
                });
            } 

        });


        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/PreEstimate/CoordFeeTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "CoordFeeId", Order: "Desc", Condition: " and IsDelete<>1 " }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'CooContent', title: '协调内容', width: 150 }
                , { field: 'CooDepart', title: '协调部门', width: 250 }
                , { field: 'money', title: '协调金额', width: 100 }
                , { field: 'Memo', title: '备注', width: 150 }
                , {
                    field: "operation", title: "操作", width: 180, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });



        table.on("tool(ProjectTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                layerindex = layer.open({
                    type: 1,
                    title: ['新增', 'font-size:18px;'],
                    area: ['900px', '300px'],
                    content: $("#EditLayer"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    success: function (layero, index) {
                        $("#EditFrom input[name='CoordFeeId']").val(data.CoordFeeId);
                        $("#EditFrom input[name='CooContent']").val(data.CooContent);
                        $("#EditFrom input[name='CooDepart']").val(data.CooDepart);
                        $("#EditFrom input[name='money']").val(data.money);
                        $("#EditFrom input[name='Memo']").val(data.Memo);
                    }
                });

            }

            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.CooContent + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index)
                    {
                        //按钮【删除】的回调
                        $.ajax({
                            type: "POST",
                            url: "/PreEstimate/CoordFeeDel",
                            async: false,
                            data: { "CoordFeeId": data.CoordFeeId },
                            success: function (msg)
                            {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                           
                                    });                                
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index)
                    {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });

    });

    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "CoordFeeId", Order: "Desc", Condition: " and IsDelete<>1"
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "CoordFeeId", Order: "Desc", Condition: " and IsDelete<>1 and CooContent like '%" + text + "%'"
                },
                page: {
                    curr: 1
                }
            });
        }
    }



    function dealNumber(money) {
        if (money && money != null) {
            money = String(money);
            var left = money.split('.')[0], right = money.split('.')[1];
            right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
            var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
            return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
        } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
            return '0.00';
        } else {
            return "";
        }
    };




</script>


